#coding=utf8

import os
import sys

__SCRIPT__PATH = os.getcwd()
sys.path.append(__SCRIPT__PATH)

import config
import fileinput

from Parser import *
from Storage import *

__year = input('Plz input the year:')
__month = input('Plz input the month:')
__day = input('Plz input the day:')
__start = input('Plz input the start line(default 1):')

LOG_FILE = '/data/weblog/%d/%d/server.pingcoo.com_%d%d%d.log' % (__year, __month, __year, __month, __day)
MARK_FILE = __SCRIPT__PATH + '/mark.ini'
MARK = '%d%d%d' % (__year, __month, __day)

# 判断日志文件是否存在
if not os.path.isfile(LOG_FILE):
    print "Sorry, the log file is not exist."
    sys.exit()

# 判断日志文件是否已分析过，防止重复分析
_handle = open(MARK_FILE, 'a+')
_content = _handle.read()
if _content and MARK in _content.split("#"):
    print 'Sorry,has been.'
    sys.exit()
else:
    _handle.write(MARK+"#")
    _handle.close()

# 以下开始正式分析日志
print 'Start Analysis Log "%s"' % (LOG_FILE)
records = []
storage = Storage()
i = 0

for log in fileinput.input(LOG_FILE):
    i += 1
    print i

    if __start and __start > i:
        continue

    parser = Parser(log, config.PARSER)
    parser_res = parser.result()
    records.append(parser_res)

    # 每1000条存储一次原始记录
    if not len(records) % 1000:
        storage.save(records)
        records = []

    # 每条记录添加一次访问分析记录
    if parser_res is not None:
        storage.insert(parser_res)

if records:
    storage.save(records)

fileinput.close()
print 'End Analysis Log "%s"' % (LOG_FILE)


